[#include "../../../common/html.ftl" /]
[@html]
    [@head title="修改角色" css=["fw", "ztree"] /]
    [@body]
        [@formMain ]
            <div class="fw-forms">
                [@dataForm formId="form-role-edit" ]
                    <input id="roleId" name="roleId" type="hidden" value="${sysRole.roleId!}"/>
                    <div class="am-form-group am-u-sm-12 am-u-md-6 am-u-lg-4">
                        <label class="is-required">角色名称：</label>
                        <input class="am-form-field am-radius" type="text" name="roleName" value="${sysRole.roleName!}" required/>
                    </div>
                    <div class="am-form-group am-u-sm-12 am-u-md-6 am-u-lg-4">
                        <label class="is-required">权限字符：</label>
                        <input class="am-form-field am-radius" type="text" name="roleKey" value="${sysRole.roleKey!}" required/>
                    </div>
                    <div class="am-form-group am-u-sm-12 am-u-md-6 am-u-lg-4">
                        <label class="is-required">显示顺序：</label>
                        <input class="am-form-field am-radius" type="text" name="roleSort" value="${sysRole.roleSort!}" required/>
                    </div>
                    <div class="am-form-group am-u-sm-12 am-u-md-6 am-u-lg-4">
                        <label class="is-required">状态：</label>
                        <select class="am-radius" name="status" required>
                            <option value="">-- 请选择 --</option>
                            [#list dict.getType("sys_role_status") as dict]
                                [#if dict.dictValue == sysRole.status ]
                                    <option value="${dict.dictValue!}" selected>${dict.dictLabel}</option>
                                [#else]
                                    <option value="${dict.dictValue!}">${dict.dictLabel}</option>
                                [/#if]
                            [/#list]
                        </select>
                    </div>
                    <div class="am-form-group am-u-sm-12 am-u-md-6 am-u-lg-4">
                        <label class="is-required">是否系统内置：</label>
                        <select class="am-radius" name="buildIn" required>
                            <option value="">-- 请选择 --</option>
                            [#list dict.getType("sys_yes_no") as dict]
                                [#if dict.dictValue == sysRole.buildIn ]
                                    <option value="${dict.dictValue!}" selected>${dict.dictLabel}</option>
                                [#else]
                                    <option value="${dict.dictValue!}">${dict.dictLabel}</option>
                                [/#if]
                            [/#list]
                        </select>
                    </div>
                    <div class="am-form-group am-u-sm-12 am-u-md-12 am-u-lg-12">
                        <label>备注：</label>
                        <textarea class="am-radius" name="remark" rows="5" >${sysRole.remark!}</textarea>
                    </div>
                    <div class="am-form-group am-u-sm-12 am-u-md-12 am-u-lg-12">
                        <label>菜单权限：</label>
                        <div id="menuTrees" class="ztree am-radius" style="border: 1px solid #ccc;"></div>
                    </div>
                [/@dataForm]
            </div>
        [/@formMain]

        [@rightToolbar /]

        [@script js=["fw", "validate", "ztree"] progress="false" ]
            <script type="text/javascript">
                var prefix = ctx + "osfw/system/manage/role";

                $(function () {
                    var url = ctx + "osfw/system/manage/menu/roleMenuTreeData?roleId=" + $("#roleId").val();
                    var options = {
                        id: "menuTrees",
                        url: url,
                        check: {enable: true},
                        expandLevel: 1
                    };
                    $.tree.init(options);
                });

                $("#form-role-edit").validate({
                    rules: {
                        roleName: {
                            remote: {
                                url: prefix + "/checkRoleNameUnique",
                                type: "post",
                                dataType: "json",
                                data: {
                                    "roleId": function () {
                                        return $("input[name='roleId']").val();
                                    },
                                    "roleName": function () {
                                        return $.common.trim($("input[name='roleName']").val());
                                    },
                                }
                            }
                        },
                        roleKey: {
                            remote: {
                                url: prefix + "/checkRoleKeyUnique",
                                type: "post",
                                dataType: "json",
                                data: {
                                    "roleId": function () {
                                        return $("input[name='roleId']").val();
                                    },
                                    "roleKey": function () {
                                        return $.common.trim($("input[name='roleKey']").val());
                                    }
                                }
                            }
                        },
                        roleSort: {
                            digits: true
                        },
                    },
                    messages: {
                        "roleName": {
                            remote: "角色名称已经存在"
                        },
                        "roleKey": {
                            remote: "角色权限已经存在"
                        }
                    },
                    onkeyup: false,
                    focusCleanup: true
                });

                function submitHandler() {
                    if ($.validate.form()) {
                        edit();
                    }
                }

                function edit() {
                    var roleId = $("input[name='roleId']").val();
                    var roleName = $("input[name='roleName']").val();
                    var roleKey = $("input[name='roleKey']").val();
                    var roleSort = $("input[name='roleSort']").val();
                    var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
                    var roleType = $("input[name='roleType']:checked").val();
                    var rolePub = $("input[name='rolePub']:checked").val();
                    var orgLevel = $("input[name='orgLevel']:checked").val();
                    var orgId = '';
                    if (rolePub == 'N'){
                        if (orgLevel == '0'){
                            orgId = $("select[name='orgIdSd'] option:selected").val();
                        }else if (orgLevel == '1'){
                            orgId = $("select[name='orgIdJd'] option:selected").val();
                        }
                    }
                    var remark = $("input[name='remark']").val();
                    var menuIds = $.tree.getCheckedNodes();
                    $.ajax({
                        cache: true,
                        type: "POST",
                        url: ctx + "osfw/system/manage/role/edit",
                        data: {
                            "roleId": roleId,
                            "roleName": roleName,
                            "roleKey": roleKey,
                            "roleSort": roleSort,
                            "status": status,
                            "roleType": roleType,
                            "rolePub": rolePub,
                            "orgLevel": orgLevel,
                            "orgId": orgId,
                            "remark": remark,
                            "menuIds": menuIds
                        },
                        async: false,
                        error: function (request) {
                            $.modal.alertError("系统错误");
                        },
                        success: function (data) {
                            $.operate.successCallback(data);
                        }
                    });
                }
            </script>
        [/@script]
    [/@body]
[/@html]